define(["PDUtilDir/grid","PDUtilDir/searchBlock","PDUtilDir/slidebar","PDUtilDir/fileupload/multiFileUpload","PDUtilDir/inputSelect","PDUtilDir/tool", 
        "PDUtilDir/util",'PDAppDir/properties',"PicView","css!picViewCss"],
		function(Grid,SearchBlock,Slidebar,MFU,inputSelect,Tool,Util,Properties){


	var uploadPotos = new Array();
    function init(){
        SearchBlock.init('searchBlockId');
        var provinces = null;
        getProvince();
        queryLevelData();
        initGrid(null);
        //初始化
     
       
    }
    var initGrid =function(param){
    	 Grid({
             id:"DemoGirdDivId",
             pageSize:10,
             index:true,
             multi:true,
             pagination : true,
             cache:false,
             layout:[
                 {name:"酒店名字",field:"name",sort:true,click:function(e){
                 	var data = e.data.row;
                     saveOrUpdate(data);
                 }},
                 {name:"代码",field:"code",sort:true},
                 {name:"电话",field:"phone",width:"10%"},
                 {name:"标签",field:"label"},
                 {name:"地址",field:"address"}
             ],
             toolbar:[
                 {name:"添加",icon:"fa fa-plus-circle",callback:function(event){
              
                     saveOrUpdate(null);
                 }},
                 {name:"删除",icon:"fa fa-trash-o",callback:function(event){
                 	deleteBySelectIds();
                 }},
                 {name:"查询",icon:"fa fa-search",callback:function(event){
                	 initGrid(getQueryParam());
                 }}
             ],
         	data : {
 				type : "URL",
 				value : getServer() + "/flea/hclv/hotel/querySelfAllForPage.htmls",
 			},
 			 queryParam:param
         });
    	 
    }
    
    
    var saveOrUpdate = function(oldData){
    	var slidebar = Slidebar({
    		size:'lg',
    		url : getServer() + "/static/app/hotel/hotelInfos/views/editHotel.html",
//    		close:true,
    		afterLoad : function($Panel) {
    			
    	
    			 var uploadFile = new Array();
    			 
    			 
    			 
    			 initFileUpload(uploadFile);
    		 	 var province = null==oldData?null:oldData.province;
    			 provinceSelect(province);
    			 var levelData = null==oldData?null:oldData.level;
    			 getLevelData(levelData);
    			
    			 
    			 getAddressByHotelId(null==oldData?null:oldData.id);
    			 var $form = $("#hotelFormId");
				 Tool.deserialize($form, oldData);
				 if(null!=oldData){
					 getAllPics(oldData.id);
				 }
				
		
    			 $("#submitBtns").on("click",function() {
    					
    				
    					//表单校验
					/*	if(!$form.valid()){
							return;
						}*/
						var hotelData = Tool.serialize("hotelFormId"); //序列化表单
						
						$.ajax({
							url:getServer()+"/flea/hclv/hotel/saveOrUpdateHotel.htmls",
							data:{'model':JSON.stringify(hotelData),'files':JSON.stringify(uploadPotos)},
							success:function(data){
								Grid.getGrid("DemoGirdDivId").refresh();
								Util.alert('成功');
								slidebar.close();
							}
						});
    			 });
    			 
    			 
    		}
    	})
    	
    }
    
    var deleteBySelectIds = function(){
    	var gdata = Grid.getGrid("DemoGirdDivId");
        var selectRows = gdata.getSelectedRow();
        if(null!=selectRows){
        	Util.confirm("是否要删除", function() {
				
        		var ids = new Array();
        		
        		$.each(selectRows,function( index, selectRow ) {
        			ids.push(selectRow.id);
        		});
        		
        		$.ajax({
        			url:getServer()+"/flea/hclv/hotel/deletByIds.htmls",
            		method:'post',
            		data:{'ids':JSON.stringify(ids)},
            		success:function(data){
            			gdata.refresh();
            			Util.alert(data.message);
            			
            		}
        		});
        		
			}, function() {
				return;
			});
        }else{
        	Util.alert('请选择要删除的数据！');
        }
    }
  
    var getProvince = function(){
    	$.ajax({
			url:getServer()+"/flea/hclv/getProvinces.htmls",
			success:function(data){
				provinces = data.data;
				 queryProvinceSelect();
			}
		});
    	
    }
    var getCity = function(pcode,initCity){
    	$.ajax({
    		url:getServer()+"/flea/hclv/getCity.htmls",
    		data:{'pcode':pcode},
    		success:function(data){
    			var citys = data.data;
    			citySelect(citys,initCity);
    		}
    	});
    	
    }
    var getZone = function(pcode,initZone){
    	$.ajax({
    		url:getServer()+"/flea/hclv/getZone.htmls",
    		data:{'pcode':pcode},
    		success:function(data){
    			var zones = data.data;
    			zoneSelect(zones,initZone);
    		}
    	});
    	
    }
    var getLevelData = function(initLevel){
    	
    	$.ajax({
    		url:getServer()+"/flea/dict/initTree.htmls",
    		data:{'dictCode':'D_HOTEL_LEVEL'},
    		success:function(data){
    			var levels = data;
    			levelSelect(levels,initLevel);
    		}
    	});
    	
    }
    
    
    
    
    var provinceSelect = function(initProvice){
    	inputSelect.dropCache();
    	inputSelect({
    		id:"provinceName",
    		key : {
    			id : "dictCode",
    			name : "dictName",
    			data : "data",
    		},
    		data :provinces,
    		simpleData : true,
    		//启用文本搜索
    		initData:[initProvice],
    		searchAble : false,
    		//选择之后回调方法：id，选择的id数组，data，选择的数据数组
    		callback : function(id, data) {
    			var str = "";
    			$.each(data, function(i, n){
    				str += n['dictCode'];
    			})
    			getCity(str);
    			$("#province").val(str);
    		}
    	});
    }
    var citySelect = function(citys,initCity){
    	inputSelect.dropCache();
    	inputSelect({
    		id:"cityName",
    		key : {
    			id : "dictCode",
    			name : "dictName",
    			data : "data",
    		},
    		data :citys,
    		simpleData : true,
    		initData:[initCity],
    		searchAble : true,
    		callback : function(id, data) {
				 var str = "";
				 $.each(data, function(i, n){
					 str += n['dictCode'];
				 });
				 getZone(str);
				 $("#city").val(str);
				 
    		 }
    	});
    }
    var zoneSelect = function(zones,initZone){
    	inputSelect.dropCache();
    	inputSelect({
    		id:"zoneName",
    		key : {
    			id : "dictCode",
    			name : "dictName",
    			data : "data",
    		},
    		data :zones,
    		searchAble : true,
    		initData:[initZone],
    		callback : function(id, data) {
    			var str = "";
    			$.each(data, function(i, n){
    				str += n.dictCode;
    			})
    			$("#area").val(str);
    		}
    	});
    }
    var levelSelect = function(leves,initLevel){
    	inputSelect.dropCache();
    	inputSelect({
    		id:"levelName",
    		key : {
    			id : "dictCode",
    			name : "dictName",
    			data : "data",
    		},
    		data :leves,
    		searchAble : true,
    		initData:[initLevel],
    		callback : function(id, data) {
    			var str = "";
    			$.each(data, function(i, n){
    				str += n.dictCode;
    			})
    			$("#level").val(str);
    		}
    	});
    }
    
    var initForUpdateSelect = function(province,city,area){
    	provinceSelect(province);
    	getCity(province,city);
    	getZone(city,area);
    }
    
    var getAddressByHotelId = function(hotelId){
    	$.ajax({
    		url:getServer()+'/flea/hclv/address/findByHotelId.htmls',
    		data:{'hotelId':hotelId},
    		async: false,
    		success:function(data){
    			var address = data.data;
    			 var $form = $("#hotelFormId");
    			
    			 
				 Tool.deserialize($form, address);
				 if(address!=null&&null!=hotelId){
					 $('#addRessId').val(address.id);
					 initForUpdateSelect(address.province,address.city,address.area);
				 }
				
    		}
    	});
    }
    var queryProvinceSelect = function(initProvice){
    	inputSelect.dropCache();
    	inputSelect({
    		id:"queryProvinceName",
    		key : {
    			id : "dictCode",
    			name : "dictName",
    			data : "data",
    		},
    		data :provinces,
    		simpleData : true,
    		//启用文本搜索
    		initData:[initProvice],
    		searchAble : false,
    		//选择之后回调方法：id，选择的id数组，data，选择的数据数组
    		callback : function(id, data) {
    			var str = "";
    			$.each(data, function(i, n){
    				str += n['dictCode'];
    			})
    			getQueryCity(str,null);
    			$("#queryProvince").val(str);
    		}
    	});
    }
    
    
    var getQueryCity = function(pcode,initCity){
    	$.ajax({
    		url:getServer()+"/flea/hclv/getCity.htmls",
    		data:{'pcode':pcode},
    		success:function(data){
    			var citys = data.data;
    			queryCitySelect(citys,initCity);
    		}
    	});
    	
    }
    
    var queryCitySelect = function(citys,initCity){
    	inputSelect.dropCache();
    	inputSelect({
    		id:"queryCityName",
    		key : {
    			id : "dictCode",
    			name : "dictName",
    			data : "data",
    		},
    		data :citys,
    		simpleData : true,
    		initData:[initCity],
    		searchAble : true,
    		callback : function(id, data) {
				 var str = "";
				 $.each(data, function(i, n){
					 str += n['dictCode'];
				 });
				 getQueryZone(str,null);
				 $("#queryCity").val(str);
				 
    		 }
    	});
    }
    
    var getQueryZone = function(pcode,initZone){
    	$.ajax({
    		url:getServer()+"/flea/hclv/getZone.htmls",
    		data:{'pcode':pcode},
    		success:function(data){
    			var zones = data.data;
    			zoneQuerySelect(zones,initZone);
    		}
    	});
    	
    }
    var zoneQuerySelect = function(zones,initZone){
    	inputSelect.dropCache();
    	inputSelect({
    		id:"queryZoneName",
    		key : {
    			id : "dictCode",
    			name : "dictName",
    			data : "data",
    		},
    		data :zones,
    		searchAble : true,
    		initData:[initZone],
    		callback : function(id, data) {
    			var str = "";
    			$.each(data, function(i, n){
    				str += n.dictCode;
    			})
    			$("#queryZone").val(str);
    		}
    	});
    }
    
    var queryLevelData = function(){
    	$.ajax({
    		url:getServer()+"/flea/dict/initTree.htmls",
    		data:{'dictCode':'D_HOTEL_LEVEL'},
    		success:function(data){
    			var levels = data;
    			queryLevelSelect(levels,null);
    		}
    	});
    }
    var queryLevelSelect = function(leves,initLevel){
    	inputSelect.dropCache();
    	inputSelect({
    		id:"queryLevelName",
    		key : {
    			id : "dictCode",
    			name : "dictName",
    			data : "data",
    		},
    		data :leves,
    		searchAble : true,
    		initData:[initLevel],
    		callback : function(id, data) {
    			var str = "";
    			$.each(data, function(i, n){
    				str += n.dictCode;
    			})
    			$("#queryLevel").val(str);
    		}
    	});
    }
    var getQueryParam = function(){
    	var param = {
    			queryName:$("#queryName").val(),
    			queryLevel:$("#queryLevel").val(),
    			queryProvince:$("#queryProvince").val(),
    			queryCity:$("#queryCity").val(),
    			queryZone:$("#queryZone").val(),
    	}
    	return param;
    }
    
    var initFileUpload = function(uploadFile){
        var settings = {
                placeAt:"multiFileUpload",
                /*
                1.0.2版本新增
                extendColumn:[
                    {name:"文件分类",filed:"category",format:function(file){return file.name}},
                    {name:"备注",filed:"remark",format:function(file){return '机密'}}
                ],*/
                //data:["5a50d697c0af806aa01e06120d04f977","6c01e4492f264befab4c0532811f8dff"],
                onUploadSuccess:function(file){
                              	uploadPotos.push({'resouceId':file.swordFileId,'path':file.swordFilePath});
                },
                onDeleteSavedFile:function(file){
                }
            };
        var MFUpload = MFU.init(settings);
    }
    var initPicViewr = function(settings){
    	var id = settings.placeAt;
    	var fileList = settings.fileLists;
    	for(var i=0;i<fileList.length;i++){
    		var file = fileList[i];
    		$('#'+id).append('<li><img data-original='+Properties.URL_FILE_SERVER_URL+file.path+' src="'+Properties.URL_FILE_SERVER_URL+file.path+'" alt="sss"></li>');
    	}
    }
    
    var getAllPics = function(uuid){
    	$.ajax({
    		url:getServer()+'/flea/hclv/resRelation/getFilePathByUuid.htmls',
    		data:{'uuid':uuid},
    		success:function(data){
    		    var viewerSettings = {
    		    		placeAt:'dowebok',
    		    		fileLists:data,
    		    		
    		    };
    			initPicViewr(viewerSettings);
    			 var viewer = new Viewer(document.getElementById('dowebok'), {url: 'data-original'});
    		}
    	});
    }
    
    
    

    return {
        init:init
    }
});